<script lang="ts" name="TopCategory" setup>
import GoodsItem from './components/goods-item.vue'
import { computed, watch, watchEffect } from 'vue'
import useStore from '@/store'
import { useRoute } from 'vue-router'
import { storeToRefs } from 'pinia'
const { home, category } = useStore()
home.getBanners()
const route = useRoute()
console.log({ ...route })

// const id = computed(() => route.params.id as string)
// // console.log(id.value)
// id.value && category.getTopCategory(id.value)

// 路由缓存  setup 函数只会执行一次

// watch(
//   () => route.params.id,
//   value => {
//     // console.log(value)
//     // value && category.getTopCategory(value as string)
//     route.fullPath === `/category/${value}` && category.getTopCategory(value as string)
//   },
//   { immediate: true }
// )

// watchEffect 侦听 不需要 写侦听某个属性,只写用就好
watchEffect(() => {
  const id = route.params.id as string
  route.fullPath === `/category/${id}` && category.getTopCategory(id)
})
// 解构     storeToRefs
const { topCategory } = storeToRefs(category)
</script>
<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{ topCategory.name }}</XtxBreadItem>
      </XtxBread>

      <!-- banner轮播图 -->
      <XtxCarousel :list="home.list" auto :time="2000" style="height: 500px" />
      <!-- {{ $route.params.id }} -->

      <!-- 全部分类 -->

      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="child in topCategory.children" :key="child.id">
            <a href="javascript:;">
              <img :src="child.picture" />
              <p>{{ child.name }}/p></p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 每一个分类 -->
      <div class="ref-goods" v-for="child in topCategory.children" :key="child.id">
        <div class="head">
          <h3 class="">- {{ child.name }} -</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem v-for="goods in child.goods!" :key="goods.id" :goods="goods" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.top-category {
  // .container {
  //   height: 500px;
  // }

  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
